package listNodePractice;

import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public class Class347_1 {
    public  int[]  topKFrequent(int[] nums,int k){
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int a: nums
             ) {
            map.put(a,map.getOrDefault(a,0)+1);
        }
        //构建大顶堆　
        PriorityQueue<Map.Entry<Integer,Integer>> pq = new PriorityQueue<>(
                (entry1,entry2)->{
                    return entry1.getValue().compareTo(entry2.getValue());
                }
        );

        for (Map.Entry<Integer,Integer>  entry: map.entrySet()
             ) {
            pq.offer(entry);
            if (pq.size()>k){
                pq.poll();
            }
        }
        int[] res = new int[k];
        for (int i = k-1; i >=0 ; i--) {
            res[i]=pq.poll().getKey();

        }

        return res;
    }

    public static void main(String[] args) {
        Class347_1 class347_1 = new Class347_1();
        int[] nums={2,3,2,5,4,3};
        int[] ints = class347_1.topKFrequent(nums, 2);
        for (int a : ints
             ) {
            System.out.println(a);

        }
    }


}





